import React, { Component } from 'react';
import { Form, Row, Col } from 'antd';
import ShowType from '../../../routes/common/showType';
import styles from '../../../index.less';
import * as config from '../../../utils/config';

const FormItem = Form.Item;

class TabMainTableComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      sTabId: props.app.currentPane.key, /* 当前标签 */
      componentName: props.componentName, /* 组件名 */
      masterConfig: props.masterConfig, /* 配置 */
      sId: props.sId, /* 业务数据Id */
      dataList: [], /* 数据 */
      sActiveId: props.sActiveId, /* 窗体Id */
      disabled: props.disabled, /* 是否是查看状态 */
      sName: '', /* 名称 */
      sType: '', /* 类型 */
      bDispatch: '', /* 生产派工 */
      iOrder: '', /* 排序 */
    };
  }

  componentWillMount() {
    /* 获取主表数据 */
    const {
      masterConfig, sActiveId, sId, componentName, sTabId,
    } = this.state;
    const { dispatch } = this.props;
    if (sId !== undefined && sId !== '') {
      const mainId = masterConfig.sId;
      /* 表Id */
      const value = { sId, pageSize: '', pageNum: '' };
      /* 业务数据Id */
      const dataUrl = `${config.server_host}business/getBusinessDataByFormcustomId/${mainId}?sModelsId=${sActiveId}`;
      dispatch({
        type: 'content/getUrlPostData',
        payload: {
          url: dataUrl, sTabId, componentName, value, formRoute: '/sisprocessclassify', returnType: 'data',
        },
      });
      /* 获取主表数据 */
    }
    if (masterConfig.gdsconfigformslave !== undefined) {
      const mainConfig = masterConfig.gdsconfigformslave;
      this.setState({
        sId: sId !== undefined ? sId : '',
        sName: mainConfig.filter(item => (item.sName.toLowerCase() === 'sname'))[0],
        sType: mainConfig.filter(item => (item.sName.toLowerCase() === 'stype'))[0],
        bDispatch: mainConfig.filter(item => (item.sName.toLowerCase() === 'bdispatch'))[0],
        iOrder: mainConfig.filter(item => (item.sName.toLowerCase() === 'iorder'))[0],
      });
    }
  }

  componentWillReceiveProps(nextProps) {
    const {
      sId, disabled, content, dataSet,
    } = nextProps;
    const { dataList } = content;
    const {
      sDataListTabId, returnType, urlData, loading, componentName,
    } = dataList;
    this.setState({ disabled, dataList: dataSet });
    if (this.state.sTabId === sDataListTabId && this.state.sTabId === nextProps.app.currentPane.key
      && this.state.componentName === componentName) {
      if (loading) return;
      if (returnType === 'data') {
        this.setState({
          dataList: urlData,
          sId: sId !== undefined ? sId : '',
        });
      }
    }
  }

  shouldComponentUpdate(nextProps, nextState) {
    const { componentName } = nextProps;
    const { sId } = this.props;
    if (sId === undefined) {
      return true;
    }
    let flag = true;
    if (nextState.disabled !== this.state.disabled) {
      return true;
    }
    if (this.state.sTabId === nextProps.app.currentPane.key &&
      this.state.componentName === componentName) {
      flag = true;
    } else {
      flag = false;
    }
    return flag;
  }

  render() {
    const {
      app, form, handleModalPane, dataName, dataType, onClose,
    } = this.props;
    const {
      sName, sType, bDispatch, iOrder,
    } = this.state;
    const {
      dataList, sId, sActiveId, disabled,
    } = this.state;
    const childProps = {
      app,
      form,
      sActiveId,
      dataList,
      sId,
      disabled,
      handleModalPane,
      dataName,
      dataType,
      onClose,
    };
    const sNameProps = { showConfig: sName, ...childProps };
    const sTypeProps = { showConfig: sType, ...childProps };
    const bDispatchProps = { showConfig: bDispatch, ...childProps };
    const iOrderProps = { showConfig: iOrder, ...childProps };
    const formItemLayout = { labelCol: { span: 12 }, wrapperCol: { span: 12 } };
    return (
      <FormItem className={styles.searchForm}>
        <Row type="flex" justify="space-between">
          <Col span={7}>
            <ShowType sName="sName" showDefaultName="名称" {...sNameProps} />
            <ShowType sName="bDispatch" useParentName showDefaultName="生产派工" {...bDispatchProps} formItemLayout={formItemLayout} />
          </Col>
          <Col span={7}>
            <ShowType sName="sType" showDefaultName="类型" {...sTypeProps} />
            <ShowType sName="iOrder" showDefaultName="排序" {...iOrderProps} />
          </Col>
        </Row>
      </FormItem>
    );
  }
}

export default TabMainTableComponent;
